import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.regex.*;

public class DataExtractor {

    public static void main(String[] args) {
        String tempDirPath = "G:\\EpiDev\\JMPeripheral\\_test\\SunWei\\JMG4PatchsV2\\03_uploadYmShotUndoByList\\重复剂次名单 2024-08-23";
        String outputPath = "G:\\EpiDev\\JMPeripheral\\_test\\SunWei\\JMG4PatchsV2\\03_uploadYmShotUndoByList\\childNoBactIDList.txt";
        String logPath = "C:\\invalid_data.log";

        // 用于存储有效的数据行
        Set<String> validLines = new LinkedHashSet<>();

        // 获取目录下的所有.txt文件
        File tempDirectory = new File(tempDirPath);
        File[] txtFiles = tempDirectory.listFiles((dir, name) -> name.toLowerCase().endsWith(".txt"));

        if (txtFiles == null) {
            System.err.println("无法找到指定目录或目录为空！");
            return;
        }

        // 处理每个文件
        for (File file : txtFiles) {
            try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    if (!line.trim().isEmpty() && isValid(line)) {
                        validLines.add(line);
                    } else if (!line.trim().isEmpty()) {
                        // 记录不符合格式的行到日志文件
                        appendToFile(logPath, line + "\n");
                    }
                }
            } catch (IOException e) {
                System.err.println("读取文件 " + file.getName() + " 时发生错误: " + e.getMessage());
            }
        }

        // 将有效数据行排序并输出到文件
        List<String> sortedValidLines = new ArrayList<>(validLines);
        Collections.sort(sortedValidLines);
        writeToFile(outputPath, sortedValidLines);
    }

    private static boolean isValid(String line) {
        String[] parts = line.split("\\.");
        if (parts.length != 3) return false;

        String idPart = parts[0];
        String codePart = parts[1];
        String datePart = parts[2];

        // 检查 ID 部分是否为 18 位数字
        if (idPart.length() != 18 || !idPart.matches("\\d{18}")) return false;

        // 检查代码部分是否为 "B" 加上三位数字
        if (!codePart.startsWith("B") || codePart.length() != 4 || !codePart.substring(1).matches("\\d{3}")) return false;

        // 检查日期部分是否为正确的格式
        if (!datePart.matches("\\d{4}-\\d{2}-\\d{2}")) return false;

        return true;
    }

    private static void appendToFile(String filePath, String content) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, true))) {
            writer.write(content);
        } catch (IOException e) {
            System.err.println("向日志文件 " + filePath + " 写入时发生错误: " + e.getMessage());
        }
    }

    private static void writeToFile(String filePath, List<String> lines) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            for (String line : lines) {
                writer.write(line);
                writer.newLine();
            }
        } catch (IOException e) {
            System.err.println("向输出文件 " + filePath + " 写入时发生错误: " + e.getMessage());
        }
    }
}
